﻿using UnityEngine;

namespace JJYYCore
{
    public class PlayerDataManager
    {
        public void SavePlayerData<T>(string id,T data) where T:PlayerDataBase
        {
            var jsonData = JsonUtility.ToJson(data);
            PlayerPrefs.SetString(id,jsonData);
        }

        public bool HasData(string dataID) => PlayerPrefs.HasKey(dataID);

        public T LoadPlayerData<T>(string id) where T: PlayerDataBase
        {
            if(!PlayerPrefs.HasKey(id))
            {
                Debug.LogError("尝试加载数据时,PlayerPrefs为包含名为:"+id+"的数据");
                return null;
            }
            var jsonData = PlayerPrefs.GetString(id);
            T data = JsonUtility.FromJson<T>(jsonData);
            return data;
        }

        public PlayerDataManager()
        {

        }
    }
}